// pages/nurse/nurse.js
const DB = wx.cloud.database().collection('article');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    search: '',
    listData: [],
  },
  skip: 0,
  field:"冷知识",
  listData:[],
  search:function(e){
    wx.showToast({
      title: '正在努力查找中，请稍等。',
      icon:"none"
    })
    DB.where({
      content:{
        $regex: '.*' + e.detail.value.keyword + '.*',
        $options:'i'
      }
      })
      .get()
      .then(res=>{
        console.log(res.data)
        if(res.data.length!=0){
          this.setData({ listData: res.data })
        }
        else{
          wx.showToast({
            title: '对不起主人，没找到相关文章…',
            icon:"none"
          })
        }
      })
  },
  top: function () {
    wx.pageScrollTo({
      scrollTop: 0,
    })
  },
  tabSelect:function(e){
    this.listData = []
    this.skip = 0 
    this.field = e.currentTarget.dataset['index']
    this.getData(e.currentTarget.dataset['index'],this.skip)
  },
  getData: function (field,skip) {
    wx.showLoading({
      title: '努力搜文章中……',
    })
    if (!field) { field="冷知识"}
    DB.where({
      type: field
    })
      .skip(skip)
      .get({
        success: res => {
          wx.hideLoading()
          this.listData = this.listData.concat(res.data)
          this.setData({
            listData: this.listData,
            tabCur: field
          })
          this.skip += 20
        }
      })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData('',this.skip)
  },

  onReachBottom: function () {
    this.skip +=20
    this.getData(this.field,this.skip);
  },
  /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
  onPullDownRefresh: function () {
    this.getData(res => {
      wx.stopPullDownRefresh();
      this.skip = 0;
    });

  },
})